package com.geek.framework.study.reactive.quickstart;


import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class SimplePublisher<T> implements Publisher<T> {

    private final List<Subscriber<? super T>> subscribers = new CopyOnWriteArrayList<>();

    @Override
    public void subscribe(Subscriber<? super T> s) {
        subscribers.add(s);
        s.onSubscribe(new SubscriptionAdapter(s));
    }


    public void publish(T data) {
        for (Subscriber<? super T> subscriber : subscribers) {
            subscriber.onNext(data);
        }
    }

}
